package com.soft.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.soft.entity.*;
import com.soft.service.TCartService;
import com.soft.service.TOrderService;
import com.soft.service.TOrderitemService;
import com.soft.service.TProductService;
import com.soft.utils.GetDate;
import com.soft.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (TOrder)表控制层
 *
 * @author makejava
 * @since 2021-12-26 16:40:34
 */
@Controller
@RequestMapping("tOrder")
public class TOrderController {
    private TUser login;
    /**
     * 服务对象
     */
    @Resource
    private TOrderService tOrderService;
    @Resource
    private TOrderitemService tOrderitemService;
    @Resource
    private TProductService tProductService;
    @Resource
    private TCartService tCartService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TOrder selectOne(Integer id) {
        return this.tOrderService.queryById(id);
    }
    @GetMapping("createOrder")
    @ResponseBody
    public String createOrder(String car, String name, String phone,
                              String address, String delivery, String code, Float totalMoney,
                              Model model, HttpSession session){
        login = (TUser) session.getAttribute("user");
        System.out.println("=======================================");
        System.out.println(car);
        System.out.println(name);
        Date date= new Date(System.currentTimeMillis());
        List<TCart> list = JSONArray.parseArray(car, TCart.class);
        String order_code = StringUtils.stransferDeliveryName(delivery) + GetDate.getCode( date )+phone.substring( 7,11 )+login.getId();//session
        TOrder tOrder = new TOrder(null, login.getId(), date, date, null,
                address,name, phone, code,
                order_code, date, date, null,
                totalMoney,totalMoney, delivery, null ,null);
        System.out.println(tOrder.toString());
        System.out.println("=======cartlist===========");
        System.out.println(car);

        tOrder = tOrderService.insert(tOrder);
//        model.addAttribute("orderId", );
        for (TCart tCart: list){
            TProduct tProduct = tProductService.queryById(tCart.getProductId());
            TOrderitem tOrderitem = new TOrderitem( null,date,date,tOrder.getId(), null,
                                                        tCart.getCount() ,tCart.getProductId(), tProduct.getPhoto(),
                                                        tProduct.getTitle(),tProduct.getPrice()*tProduct.getDiscount());
            tOrderitemService.insert(tOrderitem);
        }
        return tOrder.getId()+"";
    }
    //生成订单
//    @RequestMapping("createOrder")//name=XXX&phone=&address=&delivery=&code=中通快递
//    @ResponseBody
//    public String topay(String name, String phone,
////                        String address, String delivery, String code, Float totalMoney,
//                        String cartList,
//                        Model model,HttpSession session){
//        Date date= new Date(System.currentTimeMillis());
//        List<TCart> list = JSONArray.parseArray(cartList, TCart.class);
//
//
//        String order_code = StringUtils.stransferDeliveryName(delivery) + GetDate.getCode( date )+phone.substring( 7,11 )+4;//session
//        TOrder tOrder = new TOrder(null, 4, date, date, null,
//                address,name, phone, code,
//                order_code, date, date, null,
//                totalMoney,totalMoney, delivery, null);
//        System.out.println(tOrder.toString());
//        System.out.println("=======cartlist===========");
//        System.out.println(cartList);
//
//        tOrder = tOrderService.insert(tOrder);
//
//        for (TCart tCart: list){
//            TOrderitem tOrderitem = new TOrderitem(null,date,date,tCart.getCount(),tOrder.getId() ,tCart.getProductId(),null);
//            tOrderitemService.insert(tOrderitem);
//        }
//        return "ok";
//    }
    @GetMapping("listorder")
    public void listorder() {

    }
    @GetMapping("admin/orders")
//    @ResponseBody
    public  String orders(Integer offset, Integer limit, String search, Model model){
        PageInfo<TOrder> pageInfo = tOrderService.vagueFindAllOrders(offset, limit, null);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/orders";
//        return pageInfo;
    }
}